﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasEducationMajorDAL : IMasEducationMajorDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

        public MasEducationMajorDAL()
        {

        }
        public DataSet LoadEducationMajors(MasEducationMajorInfo mjInfo)
        {
            string strSQL;

            DataSet ds = null;

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", mjInfo.Sequence);
                dbManager.AddParameters(2, "@Filter", mjInfo.strFilter);

                strSQL = "SELECT EDU_MAJOR_SEQ, EDU_MAJOR_CODE, EDU_MAJOR_NAME, EDU_MAJOR_NAME_ENG"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_EDU_MAJORS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (mjInfo.Sequence > 0 ? " AND EDU_MAJOR_SEQ = @Sequence" : "")
                    //+ " AND CASE WHEN @Filter = '' THEN 0 = 0"
                    //    + " ELSE (EDU_MAJOR_NAME LIKE CONCAT('%', @Filter , '%')"
                    //    + ") END";
                    + (mjInfo.strFilter == "" ? "" : " AND EDU_MAJOR_NAME LIKE CONCAT('%', @Filter , '%')");
                ds = dbManager.ExecuteDataSet(CommandType.Text, strSQL);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return ds;
        }
        public IList<MasEducationMajorInfo> ListEducationMajors(MasEducationMajorInfo mjInfo)
        {
            string strSQL;

            List<MasEducationMajorInfo> infoList = new List<MasEducationMajorInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Seq", mjInfo.Sequence);

                strSQL = "SELECT EDU_MAJOR_SEQ, EDU_MAJOR_CODE, EDU_MAJOR_NAME, EDU_MAJOR_NAME_ENG"
                        + ", RECORD_STATUS, CREATED_BY, UPDATED_BY"
                    + " FROM MAS_EDU_MAJORS"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                    + (mjInfo.Sequence > 0 ? " AND EDU_MAJOR_SEQ = @Seq" : "");

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasEducationMajorInfo Info = new MasEducationMajorInfo(rdr.GetInt32(0)
                            , rdr.IsDBNull(1) ? "" : rdr.GetString(1)
                            , rdr.IsDBNull(2) ? "" : rdr.GetString(2)
                            , rdr.IsDBNull(3) ? "" : rdr.GetString(3)
                            , rdr.IsDBNull(4) ? "" : rdr.GetString(4)
                            , rdr.IsDBNull(5) ? (int?)null : rdr.GetInt32(5)
                            , rdr.IsDBNull(6) ? (int?)null : rdr.GetInt32(6));
                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
            return infoList;
        }

        public void Insert(MasEducationMajorInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Code", Info.Code);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_EDU_MAJORS(EDU_MAJOR_CODE, EDU_MAJOR_NAME, EDU_MAJOR_NAME_ENG"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                     + " VALUES(@Code, @Name, @NameEng, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MasEducationMajorInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(7);

                dbManager.AddParameters(0, "@Sequence", Info.Sequence);
                dbManager.AddParameters(1, "@Code", Info.Code);
                dbManager.AddParameters(2, "@Name", Info.Name);
                dbManager.AddParameters(3, "@NameEng", Info.NameEng);
                dbManager.AddParameters(4, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(5, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(6, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_MAJORS"
                        + " SET EDU_MAJOR_CODE = @Code"
                            + ", EDU_MAJOR_NAME = @Name"
                            + ", EDU_MAJOR_NAME_ENG = @NameEng"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                     + " WHERE EDU_MAJOR_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(int Sequence, int UpdatedBy)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Sequence", Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_EDU_MAJORS"
                        + " SET RECORD_STATUS = @RecordStatus"
                            + ", UPDATED_BY = @UpdatedBy"
                            + ", UPDATED_WHEN = @UpdatedWhen"
                     + " WHERE EDU_MAJOR_SEQ = @Sequence";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
